package com.youcash.ZyWallet_spring.service.impl;

import com.youcash.ZyWallet_spring.dao.AdminInfoMapper;
import com.youcash.ZyWallet_spring.entity.AdminInfo;
import com.youcash.ZyWallet_spring.entity.CustomersInfo;
import com.youcash.ZyWallet_spring.service.AdminInfoService;
import com.youcash.ZyWallet_spring.utils.MD5Utils;
import com.youcash.ZyWallet_spring.vo.ResStatus;
import com.youcash.ZyWallet_spring.vo.ResultVO;

import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class AdminInfoServiceImpl implements AdminInfoService {

    @Autowired
    private AdminInfoMapper adminInfoMapper;

    @Override
    public ResultVO adminRegister(String phone, String pwd) {
        synchronized (this) {
            //1.根据用户查询，这个用户是否已经被注册
            Example example = new Example(AdminInfo.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("phone", phone);
            List<AdminInfo> adminInfos = adminInfoMapper.selectByExample(example);
            //2.如果没有被注册则进行保存操作
            if (adminInfos.size() == 0) {
                String md5Pwd = MD5Utils.md5(pwd);
                AdminInfo admin = new AdminInfo();
                admin.setAdmin("某某某");
                admin.setPhone(phone);
                admin.setPassword(md5Pwd);
                admin.setAvatar("img/default.png");
                admin.setSex("男");
                admin.setStatus("1");
                admin.setRegtime(new Date());
                admin.setUpdatetime(new Date());
                int i = adminInfoMapper.insertUseGeneratedKeys(admin);
                if (i > 0) {
                    return new ResultVO(ResStatus.OK, "注册成功！", admin);
                } else {
                    return new ResultVO(ResStatus.NO, "注册失败！", null);
                }
            } else {
                return new ResultVO(ResStatus.NO, "用户名已经被注册！", null);
            }
        }
    }

    @Override
    public ResultVO checkLogin(String phone, String pwd) {
        //1.根据用户查询，这个用户是否已经被注册
        Example example = new Example(AdminInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone", phone);
        List<AdminInfo> adminInfos = adminInfoMapper.selectByExample(example);
        if (adminInfos.size() == 0) {
            return new ResultVO(ResStatus.NO, "登录失败，用户名不存在！", null);
        } else {
            String md5Pwd = MD5Utils.md5(pwd);
            if (Objects.equals(md5Pwd, adminInfos.get(0).getPassword())) {
                //如果登录成功，则需要生成令牌token
                //使用jwt规则生成token字符串
                JwtBuilder builder = Jwts.builder();
                HashMap<String, Object> map = new HashMap<>();
                map.put("key1", "value1");
                map.put("key2", "value2");
                String token = builder
                        .setSubject(phone + pwd)                             //主题，就是token中携带的数据
                        .setIssuedAt(new Date())                      //设置token的生成时间
                        .setId(adminInfos.get(0).getId() + "")    //设置用户id为token  id
                        .setClaims(map)                               //map中可以存放用户的角色权限信息
                        .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))  //设置token过期时间(24小时)
                        .signWith(SignatureAlgorithm.HS256, "com.youcash.ZyWallet_spring")       //设置加密方式和加密密码
                        .compact();
                return new ResultVO(ResStatus.OK, token, adminInfos.get(0));
            } else {
                return new ResultVO(ResStatus.NO, "登录失败，密码错误！", null);
            }
        }
    }

    @Override
    public ResultVO getById(int id) {
        //1.根据用户id查询个人信息
        Example example = new Example(CustomersInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);
        List<AdminInfo> adminInfos = adminInfoMapper.selectByExample(example);
        if (adminInfos.size() != 0) {
            return new ResultVO(ResStatus.OK, "查询成功！", adminInfos.get(0));
        } else {
            return new ResultVO(ResStatus.NO, "查询失败", null);
        }
    }
}
